package edu.gatech.doi.spamframework.filters;


import java.io.File;
 
import weka.attributeSelection.ChiSquaredAttributeEval;
import weka.attributeSelection.GainRatioAttributeEval;
import weka.attributeSelection.InfoGainAttributeEval;
import weka.attributeSelection.Ranker;
import weka.classifiers.Classifier;
import weka.core.Instances;
import weka.core.converters.ArffLoader;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.NumericToNominal;
import weka.filters.unsupervised.attribute.Remove;
import weka.filters.unsupervised.attribute.StringToWordVector;
 
public class RankAttributes {
	
	public static void rankByInfoGain(Instances trainIns)
	{
		 Ranker rank = new Ranker();
         InfoGainAttributeEval eval = new InfoGainAttributeEval();
         
         try {
			eval.buildEvaluator(trainIns);
			 int[] attrIndex = rank.search(eval, trainIns);
		        
		      
	         StringBuffer attrIndexInfo = new StringBuffer();
	         StringBuffer attrInfoGainInfo = new StringBuffer();
	         attrIndexInfo.append("Selected attributes:");
	         attrInfoGainInfo.append("Ranked attributes:\n");
	         for(int i = 0; i < attrIndex.length; i ++){
	            attrIndexInfo.append(attrIndex[i]);
	            attrIndexInfo.append(",");
	           
	            attrInfoGainInfo.append(eval.evaluateAttribute(attrIndex[i]));
	            attrInfoGainInfo.append("\t");
	            attrInfoGainInfo.append((trainIns.attribute(attrIndex[i]).name()));
	            attrInfoGainInfo.append("\n");
	         }
	         System.out.println(attrIndexInfo.toString());
	         System.out.println(attrInfoGainInfo.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         //System.out.println(rank.search(eval, trainIns));
     
        
	}
	public static void rankByGainRatio(Instances trainIns)
	{
		 Ranker rank = new Ranker();
        // InfoGainAttributeEval eval = new InfoGainAttributeEval();
         
         GainRatioAttributeEval eval=new GainRatioAttributeEval();
         try {
			eval.buildEvaluator(trainIns);
			 int[] attrIndex = rank.search(eval, trainIns);
		        
		      
	         StringBuffer attrIndexInfo = new StringBuffer();
	         StringBuffer attrInfoGainInfo = new StringBuffer();
	         attrIndexInfo.append("Selected attributes:");
	         attrInfoGainInfo.append("Ranked attributes:\n");
	         for(int i = 0; i < attrIndex.length; i ++){
	            attrIndexInfo.append(attrIndex[i]);
	            attrIndexInfo.append(",");
	           
	            attrInfoGainInfo.append(eval.evaluateAttribute(attrIndex[i]));
	            attrInfoGainInfo.append("\t");
	            attrInfoGainInfo.append((trainIns.attribute(attrIndex[i]).name()));
	            attrInfoGainInfo.append("\n");
	         }
	         System.out.println(attrIndexInfo.toString());
	         System.out.println(attrInfoGainInfo.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         //System.out.println(rank.search(eval, trainIns));
     
        
	}
	
	public static void rankByChiSquare(Instances trainIns)
	{
		 Ranker rank = new Ranker();
        // InfoGainAttributeEval eval = new InfoGainAttributeEval();
         
//         GainRatioAttributeEval eval=new GainRatioAttributeEval();
         ChiSquaredAttributeEval eval=new ChiSquaredAttributeEval();
         try {
			eval.buildEvaluator(trainIns);
			 int[] attrIndex = rank.search(eval, trainIns);
		        
		      
	         StringBuffer attrIndexInfo = new StringBuffer();
	         StringBuffer attrInfoGainInfo = new StringBuffer();
	         attrIndexInfo.append("Selected attributes:");
	         attrInfoGainInfo.append("Ranked attributes:\n");
	         for(int i = 0; i < attrIndex.length; i ++){
	            attrIndexInfo.append(attrIndex[i]);
	            attrIndexInfo.append(",");
	           
	            attrInfoGainInfo.append(eval.evaluateAttribute(attrIndex[i]));
	            attrInfoGainInfo.append("\t");
	            attrInfoGainInfo.append((trainIns.attribute(attrIndex[i]).name()));
	            attrInfoGainInfo.append("\n");
	         }
	         System.out.println(attrIndexInfo.toString());
	         System.out.println(attrInfoGainInfo.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         //System.out.println(rank.search(eval, trainIns));
     
        
	}
	
  /**
     * @param args
     */
    public static void main(String[] args) {
       // TODO Auto-generated method stub
       Instances trainIns = null;
       Instances data=null;
       try{
          
       
    	   String instancesdirectory=System.getProperties().getProperty("user.dir")+System.getProperties().getProperty("file.separator")+"instances"+System.getProperties().getProperty("file.separator");
    	   String userdir=System.getProperties().getProperty("user.dir")+System.getProperties().getProperty("file.separator");
    	   String filename=instancesdirectory+"5000_twitterperson300_300.arff";
           String stopwordsfile=userdir+"stopwords.txt";
    	   File file= new File(filename);
    	   File stopwordfile=new File(stopwordsfile);
           ArffLoader loader = new ArffLoader();
           loader.setFile(file);
            data = loader.getDataSet();
           
            /**
             * remove instanceid
             */
            Remove remove=new Remove();
            int [] attlist=new int[1];
            attlist[0]=data.numAttributes()-2;
            remove.setAttributeIndicesArray(attlist);
            
            remove.setInputFormat(data);
           Instances newdata= Filter.useFilter(data, remove);
            /**
             * string to word ,set stop words
             */
           StringToWordVector filter = new StringToWordVector();
           filter.setInputFormat(newdata);
           filter.setStopwords(stopwordfile);
           Instances dataFiltered = Filter.useFilter(newdata, filter);
           
           /**
            * numeric to nominal
            */
           NumericToNominal ntnfilter=new NumericToNominal();
           ntnfilter.setInputFormat(dataFiltered);
           Instances finaldata=Filter.useFilter(dataFiltered, ntnfilter);
           
           trainIns=finaldata;
           trainIns.setClassIndex(3);
         
           Ranker rank = new Ranker();
           InfoGainAttributeEval eval = new InfoGainAttributeEval();
        
           eval.buildEvaluator(trainIns);
           //System.out.println(rank.search(eval, trainIns));
       
           int[] attrIndex = rank.search(eval, trainIns);
          
        
           StringBuffer attrIndexInfo = new StringBuffer();
           StringBuffer attrInfoGainInfo = new StringBuffer();
           attrIndexInfo.append("Selected attributes:");
           attrInfoGainInfo.append("Ranked attributes:\n");
           for(int i = 0; i < attrIndex.length; i ++){
              attrIndexInfo.append(attrIndex[i]);
              attrIndexInfo.append(",");
             
              attrInfoGainInfo.append(eval.evaluateAttribute(attrIndex[i]));
              attrInfoGainInfo.append("\t");
              attrInfoGainInfo.append((trainIns.attribute(attrIndex[i]).name()));
              attrInfoGainInfo.append("\n");
           }
           System.out.println(attrIndexInfo.toString());
           System.out.println(attrInfoGainInfo.toString());
          
       }catch(Exception e){
           e.printStackTrace();
       }
    }
 
}